package day;

public class P583 {
    public int minDistance(String word1, String word2) {
        //求最长子字串
        int[][] dp = new int[word1.length() + 1][word2.length() + 1];
        //初始化
        for (int i = 0; i <= word2.length(); i++) {
            dp[0][i] = 0;
        }
        for (int i = 0; i <= word1.length(); i++) {
            dp[i][0] = 0;
        }
        //递推
        for (int r = 1; r <= word1.length(); r++) {
            for (int c = 1; c <= word2.length(); c++) {
                if (word1.charAt(r - 1) == word2.charAt(c - 1)) {
                    dp[r][c] = dp[r - 1][c - 1] + 1;
                } else {
                    dp[r][c] = Math.max(dp[r - 1][c], dp[r][c - 1]);
                }
            }
        }
        return word1.length() + word2.length() - 2 * dp[word1.length()][word2.length()];
    }

    public static void main(String[] args) {
        int optCount = new P583().minDistance("plasma", "altruism");
        System.out.println(optCount);
    }
}
